home *** CD-ROM | disk | FTP | other *** search
/ Java Interactive Reference Guide / Java Interactive Reference Guide.iso / autorun / source.dir / 00086_15.txt < prev    next >
Encoding:
Text File  |  1980-01-11  |  4.2 KB  |  155 lines

  1. /*
  2.  * %W% %E% Bob Weisblatt
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  8.  * without fee is hereby granted. 
  9.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  10.  * for further important copyright and trademark information and to
  11.  * http://java.sun.com/licensing.html for further important licensing
  12.  * information for the Java (tm) Technology.
  13.  * 
  14.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  15.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  18.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  19.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  20.  * 
  21.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  22.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  23.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  24.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  25.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  26.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  27.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  28.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  29.  * HIGH RISK ACTIVITIES.
  30.  */
  31.  
  32. import oak.io.InputStream;
  33. import awt.*;
  34. import browser.*;
  35. import net.www.html.*;
  36. import browser.audio.*;
  37.  
  38. /**
  39.  * A simple Applet class to play an image loop.  The "img" tag parameter
  40.  * indicates what image loop to play.
  41.  *
  42.  * @author     Bob Weisblatt (major thievery from jag, maybe wholesale is a better word.)
  43.  * @version     1.07, 19 Jan 1995
  44.  */
  45. public
  46. class JackhammerDuke extends Applet implements Runnable {
  47.     /**
  48.      * The current loop slot.
  49.      */
  50.     int seqslot = 0;
  51.  
  52.     /**
  53.      * The number of images
  54.      */
  55.     int nimgs = 4;
  56.  
  57.     /**
  58.      * A strip of 4 images.
  59.      */
  60.     Image imgs;
  61.  
  62.     /**
  63.      * The directory or URL from which the images are loaded
  64.      */
  65.     String dir;
  66.  
  67.     /**
  68.      * The thread animating the images.
  69.      */
  70.     Thread kicker = null;
  71.  
  72.     /**
  73.      * The length of the pause between revs.lkj 
  74.      */
  75.     int pause;
  76.  
  77.     /**
  78.      * The current x position.
  79.      */
  80.     double x;
  81.  
  82.     /**
  83.      * Run the image loop. This method is called by class Thread.
  84.      * @see oak.lang.Thread
  85.      *
  86.      * documentURL is the URL of the document in which the applet is
  87.      * embedded. It shouldn't be changed.
  88.      *
  89.      * dir is in net.www.html.Tag but is undocumented.
  90.      */
  91.     public void run() {
  92.     Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  93.  
  94.     imgs = getImage("images/jack.gif");
  95.  
  96.     if (imgs != null) {
  97.         x = (width - imgs.width/nimgs) / 2;
  98.         while (width > 0 && height > 0 && kicker != null) {
  99.         if (seqslot == 0) {
  100.             play("audio/jackhammer-short.au");
  101.         }
  102.  
  103.         repaint();
  104.         Thread.sleep((seqslot == sequence.length -1) ? 500 : 100);
  105.         seqslot = (seqslot + 1) % sequence.length;
  106.         }
  107.     }
  108.  
  109.     }
  110.  
  111.     private int sequence[] = { 2, 1, 0, 1, 0, 1, 0, 1, 2, 1, 0, 2, 0, 1, 0, 2, 3};
  112.  
  113.     /**
  114.      * Paint the current frame.
  115.      */
  116.     boolean erase;
  117.  
  118.     public void update(Graphics g) {
  119.     if (erase || (sequence[seqslot] == 3)) {
  120.         g.setForeground(Color.lightGray);
  121.         g.fillRect(0, 0, width, height);
  122.         erase = false;
  123.     }
  124.     paint(g);
  125.     }
  126.     public void paint(Graphics g) {
  127.     int loopslot = sequence[seqslot];
  128.     if ((imgs != null) && (loopslot < nimgs)) {
  129.         int w = imgs.width / nimgs;
  130.         x = Math.max(0, Math.min(width - w, x + Math.random() * 4 - 2));
  131.         
  132.         g.clipRect((int)x, 0, w, imgs.height);
  133.         g.drawImage(imgs, (int)x - loopslot * w, 0);
  134.         erase = (loopslot == 3);
  135.     }
  136.     }
  137.  
  138.     /**
  139.      * Start the applet by forking an animation thread.
  140.      */
  141.     public void start() {
  142.     if (kicker == null) {
  143.         kicker = new Thread(this);
  144.         kicker.start();
  145.     }
  146.     }
  147.  
  148.     /**
  149.      * Stop the applet. The thread will exit because kicker is set to null.
  150.      */
  151.     public void stop() {
  152.     kicker = null;
  153.     }
  154. }
  155.